package com.rerun.service.impl;

import com.rerun.common.bean.Rest;
import com.rerun.common.util.CommonUtil;
import com.rerun.common.util.Validator;
import com.rerun.entity.EduBackground;
import com.rerun.entity.Resume;
import com.rerun.entity.SysUser;
import com.rerun.entity.vo.StudentExcelDataVo;
import com.rerun.enums.DataFromEnum;
import com.rerun.enums.ResumeStatusEnum;
import com.rerun.service.IEduBackgroundService;
import com.rerun.service.IExcelService;
import com.rerun.service.IMemberUserService;
import com.rerun.service.IResumeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author dax.
 * @version v1.0
 * @since 2017/10/20 9:53
 */
@Service
public class ExcelServiceImpl implements IExcelService {
    @Resource
    private IEduBackgroundService iEduBackgroundService;
    @Resource
    private IResumeService iResumeService;
    @Resource
    private IMemberUserService iMemberUserService;

    @Override
    public <T>Rest dataInsert(List<T> t) throws IllegalAccessException, ParseException {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();

        Rest rest = new Rest();
        StringBuffer stringBuffer = new StringBuffer();
        if (!t.isEmpty()) {
            List<StudentExcelDataVo> wrongData = new ArrayList<>();
            List<EduBackground> eduBackgrounds = new ArrayList<>();
            List<Resume> resumes = new ArrayList<>();
            for (Object o : t) {
//         验证字段是否完整  不完整 需要标注记录
                StudentExcelDataVo vo = (StudentExcelDataVo) o;
                StudentExcelDataVo studentExcelDataVo = Validator.haveNullField(vo);
//          返回为空 说明 必传参数都有  可以执行下一步
                if (Objects.isNull(studentExcelDataVo)) {
                    String phoneNumber =  vo.getPhoneNumber();
//                    用户不存在 导入  存在则不导入
                     if (Objects.nonNull(iMemberUserService.selectByPhone(phoneNumber))){
                         EduBackground eduBackground = new EduBackground();
                         eduBackground.setPhoneNumber(phoneNumber);
                         eduBackground.setSchoolName(vo.getCollegeName());
                         eduBackground.setMajor(vo.getMajor());
                         eduBackground.setEdu(vo.getEducation());
                         eduBackground.setStartDate(CommonUtil.dateFormat(vo.getEntranceTime()));
                         eduBackground.setEndDate(CommonUtil.dateFormat(vo.getGraduationTime()));
                         eduBackground.setCreateTime(new Date());
                         eduBackground.setCollegeId(sysUser.getCompanyId());//院校主键关联
                         eduBackgrounds.add(eduBackground);

                         Resume resume = new Resume();
                         resume.setName(vo.getStudentName());
                         long sex = vo.getSex().equals("男") ? 1 : 0;
                         resume.setSex(sex);
                         resume.setPhoneNumber(phoneNumber);
                         resume.setBirthday(CommonUtil.dateFormat(vo.getBirthday()));
                         resume.setCreateTime(new Date());
                         resume.setDataFrom(DataFromEnum.COLLEGE.getType());
                         resume.setStatus(ResumeStatusEnum.SHOW.getKey());
                         resumes.add(resume);
                     }

                } else {
                    wrongData.add(studentExcelDataVo);
                }
            }
            iEduBackgroundService.insertBatch(eduBackgrounds);
            iResumeService.insertBatch(resumes);
            int successCount = t.size() - wrongData.size();
            stringBuffer.append("成功导入数据 ").append(successCount).append("条，失败 ").append(wrongData.size()).append("条");
            rest.setData(wrongData);
        }

        rest.setCode(200);
        stringBuffer.append("没有excel数据");
        rest.setMsg(stringBuffer.toString());

        return rest;
    }
}
